import axios from 'axios'
import {ElMessage} from 'element-plus'
import {baseUrl} from '../api';
let pref = baseUrl;
let token = localStorage.getItem('');
// let headersObj = {"headers": {'token': token}}
axios.defaults.headers.common['x-requested-with'] = 'XMLHttpRequest';
// axios.defaults.withCredentials = true
let loadingInstance;
axios.interceptors.request.use(
    config => {
        // config.headers['X-Requested-With'] = 'XMLserviceRequest';
        // const token = getToken();
        const token = "85d9e99f5387fe3f2d51056339fde59f";
        if (token) {
            config.headers["token"] = token;
        }

        return config
    }, function (error) {
        return Promise.reject(error)
    }
)



axios.interceptors.response.use(
    function (response) {
        return response
    }, function (error) {
        if (error.response) {
            return Promise.reject(error)
        } else {
            // window.location.href = ssoUrl +'/login?service='+ encodeURIComponent(loginControllerUrl)
        }
        // Do something with response error
    })

export function get(url, option) {

    return axios.get(`${pref}${url}`, {
        // ...headersObj,
        emulateJSON: true,
        ...option
    }).catch(function (error) {
        if (error.code > 0) {
            // alert(error.msg);
        }
        throw 0;
    })
}

export function post(url, body,option, timeout) {

    // headersObj = {"headers": {'token': localStorage.getItem('token')}}
    // if(localStorage.getItem('ticket')) {
    //     url += '?ticket=' + localStorage.getItem('ticket')
    // }


    return axios.post(`${pref}${url}`, body, {
        // ...headersObj,
        emulateJSON: true,
        ...option,
        ...timeout

    }).catch(function (error) {
        if (error.code > 0) {
            // alert(error.msg);
        }
        throw 0;
    })

}
/**
 * 放在url中
 * 封装delete请求
 * @param url
 * @param params
 * @returns {Promise}
 */
  
export function del(url,params={}){
    return new Promise((resolve,reject) => {
        axios.delete(url,{
            params:params
        })
        .then(response => {
            resolve(response.data);
        },err => {
            reject(err)
        })
    })
}
  
/**
 * 放在body中
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */
  
export function bodyDel(url,data={}){
    return new Promise((resolve,reject) => {
        axios.delete(url,data)
        .then(response => {
            resolve(response.data);
        },err => {
            reject(err)
        })
    })
}
   /**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
    return new Promise((resolve,reject) => {
        axios.put(url,data)
        .then(response => {
            resolve(response.data);
        },err => {
            reject(err)
        })
    })
}